#include "user.h"

void timer2_init(uint16_t psc,uint16_t arr)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	
	TIM_TimeBaseInitTypeDef timerStruct;
	timerStruct.TIM_ClockDivision=TIM_CKD_DIV1;
	timerStruct.TIM_CounterMode=TIM_CounterMode_Up;
	timerStruct.TIM_Period=arr-1;
	timerStruct.TIM_Prescaler=psc-1;
	TIM_TimeBaseInit(TIM2,&timerStruct);
	
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
	TIM_Cmd(TIM2,ENABLE);
	
	NVIC_InitTypeDef nvicStruct;
	nvicStruct.NVIC_IRQChannel=TIM2_IRQn;
	nvicStruct.NVIC_IRQChannelCmd=ENABLE;
	nvicStruct.NVIC_IRQChannelPreemptionPriority=2;
	nvicStruct.NVIC_IRQChannelSubPriority=2;
	NVIC_Init(&nvicStruct);
}

void TIM2_IRQHandler()
{
	if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
	{
		GPIO_ToggleBits(GPIOG,GPIO_Pin_9);
		TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
	}
}


void TIM2_close()
{
		
		TIM_ITConfig(TIM2,TIM_IT_Update,DISABLE);
		TIM_Cmd(TIM2,DISABLE);
}